/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.tedu.mall.product.webapi.service.impl.order;

import cn.tedu.mall.common.exception.CoolSharkServiceException;
import cn.tedu.mall.common.pojo.product.vo.SkuStandardVO;
import cn.tedu.mall.common.restful.ResponseCode;
import cn.tedu.mall.product.service.order.IForOrderSkuService;
import cn.tedu.mall.product.webapi.mapper.SkuMapper;
import java.util.Random;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@DubboService
@Slf4j
@Service
public class ForOrderSkuServiceImpl implements IForOrderSkuService {

    @Autowired
    private SkuMapper skuMapper;

    @Override
    public int reduceStockNum(Long skuId, Integer quantity) {
        int i = skuMapper.updateStockById(skuId, quantity);
        if (new Random().nextBoolean()){
            log.info("异常出现,事务应该回滚");
            throw new CoolSharkServiceException(ResponseCode.CONFLICT,"手动人为异常");
        }

        return i;
    }

    @Override
    public SkuStandardVO getById(Long id) {
        SkuStandardVO sku = skuMapper.getById(id);
        if (sku == null) {
            throw new CoolSharkServiceException(ResponseCode.NOT_FOUND, "获取SKU详情失败，尝试访问的数据不存在！");
        }
        return sku;
    }
}
